package com.everflourish.yeah100.utils;

import com.everflourish.yeah100.entity.clazz.ClassManage;
import com.everflourish.yeah100.entity.marking.ObjectAnswer;
import com.everflourish.yeah100.entity.marking.Standard;
import com.everflourish.yeah100.entity.marking.StandardAnswer;
import com.everflourish.yeah100.entity.marking.SubjectAnswer;
import com.everflourish.yeah100.entity.marking.Topic;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：Created by liqisi on 2016/6/22.
 * 时间：2016/6/22
 */
public class AnswerUtil {

    public static final String SERIAL_NUMBER = "serial_number";
    public static final String ANSWER = "answer";

    /**
     * 提取题型
     *
     * @param response
     * @return
     * @throws JSONException
     */
    public static ArrayList<Topic> getTopics(JSONObject response) throws JSONException {
        ArrayList<Topic> topics = null;
        if (response == null || response.isNull("topics")) {
            topics = new ArrayList<Topic>();
            return topics;
        }
        JSONArray jsonArray = response.getJSONArray("topics");
        Gson gson = new Gson();
        Type type = new TypeToken<List<Topic>>() {
        }.getType();
        topics = gson.fromJson(jsonArray.toString(), type);
        return topics;
    }

    public static float getSubTotalScore2(List<Topic> topics) {
        if (topics == null) {
            return 0f;
        }
        float subTotalScore = 0;
        for (Topic t : topics) {
            if (t.getTopicType() == 3) {
                subTotalScore += t.getTopicPoint();
            }
        }
        return subTotalScore;
    }

    /**
     * 提取标准答案
     *
     * @param response
     * @return
     * @throws JSONException
     */
    public static List<Standard> getStandardAnswers(JSONObject response)
            throws JSONException {
        List<Standard> standards = null;
        if (response == null || response.isNull("standardAnswers")) {
            standards = new ArrayList<Standard>();
            return standards;
        }
        JSONArray jsonArray = response.getJSONArray("standardAnswers");
        Gson gson = new Gson();
        Type type = new TypeToken<List<Standard>>() {
        }.getType();
        standards = gson.fromJson(jsonArray.toString(), type);
        return standards;
    }

    /**
     * 提取学生答案
     *
     * @param response
     * @return
     * @throws JSONException
     */
    public static List<ClassManage> getStudentAnswers(JSONObject response)
            throws JSONException {
        List<ClassManage> classManages = null;
        if (response == null || response.isNull("studentAnswers")) {
            classManages = new ArrayList<ClassManage>();
            return classManages;
        }
        JSONObject studentAnswersObj = response.getJSONObject("studentAnswers");
        if (studentAnswersObj.isNull("classes")) {
            classManages = new ArrayList<ClassManage>();
            return classManages;
        }
        JSONArray classesArray = studentAnswersObj.getJSONArray("classes");
        Gson gson = new Gson();
        Type type = new TypeToken<List<ClassManage>>() {
        }.getType();
        classManages = gson.fromJson(classesArray.toString(), type);
        return classManages;
    }

    /**
     * 获取客观题的总分
     *
     * @param topics
     * @return
     */
    public static float getObjTotalScore(List<Topic> topics) {
        if (topics == null) {
            return 0f;
        }
        float objTotalScore = 0;
        for (Topic t : topics) {
            if (t.getTopicType() != 3) {
                objTotalScore += (t.getTopicEnd() - t.getTopicBegin() + 1)
                        * t.getTopicPoint();
            }
        }
        return objTotalScore;
    }

    /**
     * 将content转换成List<StandardAnswer> 标准答案
     *
     * @param answerContent
     * @param topics
     * @return
     */
    public static ArrayList<StandardAnswer> answerToList(
            String answerContent, List<Topic> topics) {
        String[] answers = null;
        if (answerContent != null && !answerContent.trim().equals("")) {
            if (answerContent.endsWith(",")) {
                answerContent += " ";// 防止 ,,,,,,,切割不了
            }
            String answerStr = decryptAnswer(answerContent);
//            if (answerStr.endsWith(",")) {
//                answerStr += " ";
//            }
            answers = answerStr.split(",");
        }
        ArrayList<StandardAnswer> answerList = null;
        // int count = 0;
        if (topics != null) {
            answerList = new ArrayList<StandardAnswer>();
            for (Topic t : topics) {
                if (t.getTopicType() != 3) {// 只要不是主观题
                    Integer topicBegin = t.getTopicBegin();
                    Integer topicEnd = t.getTopicEnd();
                    for (int i = topicBegin; i <= topicEnd; i++) {
                        StandardAnswer standardAnswer = new StandardAnswer();
                        standardAnswer.setSerialNumber(i);
                        if (answers != null && answers.length <= (i - 1)) {
                            standardAnswer.setAnswer("");
                        } else {
                            standardAnswer.setAnswer(answers == null ? ""
                                    : answers[i - 1]);
                        }
                        answerList.add(standardAnswer);
                    }
                }
            }
        }
        return answerList;
    }

    /**
     * 将content转换成List<ObjectAnswer> 客观题答案
     *
     * @param answerContent
     * @param topics
     * @return
     */
    public static ArrayList<ObjectAnswer> objectAnswerToList(
            String answerContent, List<Topic> topics) {
        String[] answers = null;
        if (answerContent != null && !answerContent.trim().equals("")) {
            if (answerContent.endsWith(",")) {
                answerContent += " ";// 防止 ,,,,,,,切割不了
            }
            String answerStr = decryptAnswer(answerContent);
//            if (answerStr.endsWith(",")) {
//                answerStr += " ";
//            }
            answers = answerStr.split(",");
        }
        ArrayList<ObjectAnswer> answerList = null;
        // int count = 0;
        if (topics != null) {
            answerList = new ArrayList<ObjectAnswer>();
            for (Topic t : topics) {
                if (t.getTopicType() != 3) {// 只要不是主观题
                    Integer topicBegin = t.getTopicBegin();
                    Integer topicEnd = t.getTopicEnd();
                    for (int i = topicBegin; i <= topicEnd; i++) {
                        ObjectAnswer objectAnswer = new ObjectAnswer();
                        objectAnswer.setSerialNumber(i);
                        if (answers != null && answers.length <= (i - 1)) {
                            objectAnswer.setAnswer("");
                        } else {
                            objectAnswer.setAnswer(answers == null ? ""
                                    : answers[i - 1]);
                        }
                        answerList.add(objectAnswer);
                    }
                }
            }
        }
        return answerList;
    }

    /**
     * 将List<StandardAnswer>转换成content  标准答案
     *
     * @param standardAnswers
     * @param maxSeqno
     * @return
     */
    public static String listToAnswer(List<StandardAnswer> standardAnswers,
                                      int maxSeqno) {
        StringBuilder answerContent = new StringBuilder();
        Map<Integer, String> answerMap = new HashMap<Integer, String>();
        for (int i = 0; i < standardAnswers.size(); i++) {
            StandardAnswer standardAnswer = standardAnswers.get(i);
            answerMap.put(standardAnswer.getSerialNumber(),
                    standardAnswer.getAnswer());
        }
        for (int seqno = 1; seqno <= maxSeqno; seqno++) {
            String answer = answerMap.get(seqno);
            if (answer == null) {
                answerContent.append("0");
            } else {
                answerContent.append(answer);
            }
            if (seqno != maxSeqno) {
                answerContent.append(",");
            }
        }
        return encryptAnswer(answerContent.toString());
    }

    /**
     * 将List<ObjectAnswer>转换成content  客观题答案
     *
     * @param objectAnswers
     * @param maxSeqno
     * @return
     */
    public static String listToObjectAnswer(List<ObjectAnswer> objectAnswers,
                                            int maxSeqno) {
        StringBuilder answerContent = new StringBuilder();
        Map<Integer, String> answerMap = new HashMap<Integer, String>();
        for (int i = 0; i < objectAnswers.size(); i++) {
//            StandardAnswer standardAnswer = objectAnswers.get(i);
            ObjectAnswer objectAnswer = objectAnswers.get(i);
            answerMap.put(objectAnswer.getSerialNumber(),
                    objectAnswer.getAnswer());
        }
        for (int seqno = 1; seqno <= maxSeqno; seqno++) {
            String answer = answerMap.get(seqno);
            if (answer == null) {
                answerContent.append("0");
            } else {
                answerContent.append(answer);
            }
            if (seqno != maxSeqno) {
                answerContent.append(",");
            }
        }
        return encryptAnswer(answerContent.toString());
    }

    /**
     * 大题list转content
     *
     * @param mSubList
     * @param topics
     * @return
     */
    public static String listToSubjectAnswer(List<SubjectAnswer> mSubList,
                                             List<Topic> topics) {
        StringBuilder answerContent = new StringBuilder();
        int count = 0;
        for (int i = 1; i <= topics.size(); i++) {
            Topic topic = topics.get(i - 1);
            if (topic.getTopicType() == 3) { // 主观题
                String answer = (String) mSubList.get(count).getAnswer();
                answerContent.append(answer);
                count++;
            } else { // 不是主观题
                answerContent.append("0");
            }
            if (i != topics.size()) {
                answerContent.append(",");
            }
        }
        return answerContent.toString();
    }

    /**
     * 小题list转content
     *
     * @param list
     * @param maxSeqno
     * @return
     */
    public static String listOneToAnswer(List<SubjectAnswer> list, List<Topic> topics,
                                         int maxSeqno) {
        StringBuilder answerContent = new StringBuilder();
////        Map<Integer, String> answerMap = new HashMap<Integer, String>();
//        SubjectAnswer subjectAnswer  = new SubjectAnswer();
//        for (int i = 0; i < list.size(); i++) {
////            Map<String, Object> m = list.get(i);
//          SubjectAnswer answer = list.get(i);
////            answerMap.put((Integer) m.get(SERIAL_NUMBER),
////                    (String) m.get(ANSWER));
//            subjectAnswer.setAnswer(answer.getAnswer());
//            subjectAnswer.setTopic(an);
//        }
        List<SubjectAnswer> subjectAnswerList = new ArrayList<>();
        for (int seqno = 1; seqno <= maxSeqno; seqno++) {
            SubjectAnswer subjectAnswer = new SubjectAnswer();
            subjectAnswer.setSerialNumber(seqno);
            subjectAnswer.setAnswer("0");
            subjectAnswerList.add(subjectAnswer);
        }
        int count = 0;
        for (int i = 0; i < topics.size(); i++) {
            Topic topic = topics.get(i);
            int beginNo = topic.getTopicBegin();
            int endNo = topic.getTopicEnd();
            if (topic.getTopicType() == 3) {   // 主观题
                subjectAnswerList.remove(beginNo - 1);
                SubjectAnswer answer = new SubjectAnswer();
                answer.setAnswer(list.get(count).getAnswer());
                answer.setSerialNumber(beginNo);
                subjectAnswerList.add(beginNo - 1, answer);
                count++;
            }

//            for (int j = 0; j < subjectAnswerList.size(); j++) {
//                SubjectAnswer subjectAnswer = subjectAnswerList.get(j);
////                Map<String, Object> m = list.get(j);
//
//                answerMap.put((Integer) m.get(SERIAL_NUMBER),
//                        (String) m.get(ANSWER));
//            }
//            String answer = list.get(seqno).getAnswer();
//            if (answer == null) {
//                answerContent.append("0");
//            } else {
//                answerContent.append(answer);
//            }
//            if (seqno != maxSeqno) {
//                answerContent.append(",");
//            }
        }
        for (int seqno = 1; seqno <= maxSeqno; seqno++) {
            String answer = subjectAnswerList.get(seqno - 1).getAnswer();
            if (answer == null) {
                answerContent.append("0");
            } else {
                answerContent.append(answer);
            }
            if (seqno != maxSeqno) {
                answerContent.append(",");
            }
        }
//        StringBuilder answerContent = new StringBuilder();
//        int count = 0;
//        for (int i = 0; i < topics.size(); i++) {
//            Topic topic = topics.get(i);
//            if (topic.getTopicType() == 3) {  // 主观题
//                int beginNo = topic.getTopicBegin();
//                int endNo = topic.getTopicEnd();
//                if (beginNo == endNo) {  //
//                    answerContent.append(list.get(count).getAnswer());
//                    count++;
//                    if (topic.getTopicEnd() != maxSeqno) {
//                        answerContent.append(",");
//                    }
//                } else {
//                    for (int j = beginNo; j <= endNo; j++) {
//                        if (j == beginNo) {
//                            if (list.get(count).getAnswer().equals("")) {
//                                answerContent.append("0");
//                            } else {
//                                answerContent.append(list.get(count).getAnswer());
//                            }
//                            count++;
//                        } else {
//                            answerContent.append("0");
//                        }
//                        if (topic.getTopicEnd() != maxSeqno) {
//                            answerContent.append(",");
//                        }
//                    }
//                }
//            } else {  //  客观题
//                int beginNo = topic.getTopicBegin();
//                int endNo = topic.getTopicEnd();
//                if (beginNo == endNo) {  //
//                    answerContent.append("0");
//                    if (topic.getTopicEnd() != maxSeqno) {
//                        answerContent.append(",");
//                    }
//                } else {
//                    for (int j = beginNo; j <= endNo; j++) {
//                        answerContent.append("0");
//                        if (topic.getTopicEnd() != maxSeqno) {
//                            answerContent.append(",");
//                        }
//                    }
//                }
//            }
//        }
        return answerContent.toString();
    }

    /**
     * 将content转换成List<Map<String,Object>>
     *
     * @param answerMap
     * @param topics
     * @return
     */
    public static ArrayList<Map<String, Object>> mapToList(
            Map<String, Object> answerMap, List<Topic> topics) {
        ArrayList<Map<String, Object>> list = null;
        if (topics != null) {
            list = new ArrayList<Map<String, Object>>();
            for (Topic t : topics) {
                if (t.getTopicType() != 3) {// 只要不是主观题
                    Integer topicBegin = t.getTopicBegin();
                    Integer topicEnd = t.getTopicEnd();
                    for (int i = topicBegin; i <= topicEnd; i++) {
                        Map<String, Object> map = new HashMap<String, Object>();
                        List<String> answerList = (List<String>) answerMap
                                .get(i + "");
                        String answer = "";
                        if (answerList != null) {
                            for (String s : answerList) {
                                answer += s;
                            }
                        }
                        map.put(SERIAL_NUMBER, i);
                        map.put(ANSWER, answer == null ? "" : answer);
                        list.add(map);
                    }
                }
            }
        }
        return list;
    }

    /**
     * 将List<Map<String,Object>>转换成content
     *
     * @param list
     * @return
     */
    public static String listToStudentAnswer(List<Map<String, Object>> list,
                                             int maxSeqno) {
        StringBuilder answerContent = new StringBuilder();
        Map<Integer, String> answerMap = new HashMap<Integer, String>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> m = list.get(i);
            answerMap.put((Integer) m.get(SERIAL_NUMBER),
                    (String) m.get(ANSWER));
        }
        for (int seqno = 1; seqno <= maxSeqno; seqno++) {
            String answer = answerMap.get(seqno);
            if (answer == null) {
                answerContent.append("0");
            } else {
                answerContent.append(answer);
            }
            if (seqno != maxSeqno) {
                answerContent.append(",");
            }
        }
        return encryptAnswer(answerContent.toString());
    }

    /**
     * 将content转换成List<Map<String,Object>>
     *
     * @param answerContent
     * @param topics
     * @return
     */
    public static ArrayList<Map<String, Object>> answerToStudentList(
            String answerContent, List<Topic> topics) {
        String[] answers = null;
        if (answerContent != null && !answerContent.trim().equals("")) {
            if (answerContent.endsWith(",")) {
                answerContent += " ";// 防止 ,,,,,,,切割不了
            }
            String answerStr = decryptAnswer(answerContent);
//            if (answerStr.endsWith(",")) {
//                answerStr += " ";
//            }
            answers = answerStr.split(",");
        }
        ArrayList<Map<String, Object>> list = null;
        // int count = 0;
        if (topics != null) {
            list = new ArrayList<Map<String, Object>>();
            for (Topic t : topics) {
                if (t.getTopicType() != 3) {// 只要不是主观题
                    Integer topicBegin = t.getTopicBegin();
                    Integer topicEnd = t.getTopicEnd();
                    for (int i = topicBegin; i <= topicEnd; i++) {
                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put(SERIAL_NUMBER, i);
                        if (answers != null && answers.length <= (i - 1)) {
                            map.put(ANSWER, "");
                        } else {
                            map.put(ANSWER, answers == null ? ""
                                    : answers[i - 1]);
                        }
                        list.add(map);
                        // count++;
                    }
                }
            }
        }
        return list;
    }

    /**
     * 主观题整体录入切割
     *
     * @param answerContent
     * @param topics
     * @return
     */
    public static ArrayList<SubjectAnswer> answerSubOneList(
            String answerContent, List<Topic> topics) {
        ArrayList<SubjectAnswer> list = null;
        if (topics != null) {
//            list = new ArrayList<Map<String, Object>>();
            list = new ArrayList<SubjectAnswer>();

            // String content = mStudent.getAnswer().getSubjectAnswers();
            String[] answers = null;
            if (!StringUtil.stringIsNull(answerContent)) {
                answers = answerContent.split(",");
            }
//			int j = 0;
            for (int i = 0; i < topics.size(); i++) {
                Topic topic = topics.get(i);
                if (topic.getTopicType() == 3) {
//                    if (!StringUtil.stringIsNull(answerContent)) {
//                        Map<String, Object> map = new HashMap<String, Object>();
                    SubjectAnswer subjectAnswer = new SubjectAnswer();
//                        map.put("subject", topic);
                    subjectAnswer.setTopic(topic);
                    subjectAnswer.setSerialNumber(topic.getTopicBegin());
                    if (answers != null && answers.length <= topic.getTopicBegin() - 1) {
//                            map.put("answer", "");
                        subjectAnswer.setAnswer("");
                    } else {
//                            map.put("answer", answers == null ? ""
//                                    : answers[i]);
                        subjectAnswer.setAnswer(answers == null ? "" : answers[topic.getTopicBegin() - 1]);

                    }
                    list.add(subjectAnswer);
//                    }
                }
            }
        }
        return list;
    }

    /**
     * 主观题答案逐题切割
     *
     * @param answerContent
     * @param topics
     * @return
     */
    public static ArrayList<SubjectAnswer> answerSubToList(
            String answerContent, List<Topic> topics) {
        String[] answers = null;
        if (answerContent != null && !answerContent.trim().equals("")) {
            if (answerContent.endsWith(",")) {
                answerContent += "  ";// 防止 ,,,,,,,切割不了
            }
            answers = answerContent.split(",");
        }
        ArrayList<SubjectAnswer> list = null;
        // int count = 0;
        if (topics != null) {
            list = new ArrayList<SubjectAnswer>();
            for (Topic t : topics) {
                if (t.getTopicType() == 3) {// 只要是主观题
                    Integer topicBegin = t.getTopicBegin();
                    Integer topicEnd = t.getTopicEnd();
                    for (int i = topicBegin; i <= topicEnd; i++) {
//                        Map<String, Object> map = new HashMap<String, Object>();
                        SubjectAnswer subjectAnswer = new SubjectAnswer();
                        subjectAnswer.setSerialNumber(i);
//                        map.put(SERIAL_NUMBER, i);
                        if (answers != null && answers.length <= (i - 1)) {
//                            map.put(ANSWER, "");
                            subjectAnswer.setAnswer("");
                        } else {
                            subjectAnswer.setAnswer(answers == null ? ""
                                    : answers[i - 1]);
//                            map.put(ANSWER, answers == null ? ""
//                                    : answers[i - 1]);
                        }
                        list.add(subjectAnswer);
                        // count++;
                    }
                }
            }
        }
        return list;
    }

    /**
     * 获取学生的成绩
     *
     * @param topics
     * @param staList
     * @param stuList
     * @return
     */
    public synchronized static float getStudentGrade(List<Topic> topics,
                                                     List<Map<String, Object>> staList, List<Map<String, Object>> stuList) {
        float studentGrage = 0;
        if (topics == null || staList == null || stuList == null) {
            return studentGrage;
        }
        for (int i = 0; i < staList.size(); i++) {// 遍历标准
            Map<String, Object> m = staList.get(i);
            String staAnswer = (String) m.get(ANSWER);
            int staTopicNo = (Integer) m.get(SERIAL_NUMBER);
            for (int j = 0; j < stuList.size(); j++) {// 遍历学生的答案
                Map<String, Object> stuMap = stuList.get(j);
                String stuAnswer = (String) stuMap.get(ANSWER);
                int stuTopicNo = (Integer) stuMap.get(SERIAL_NUMBER);
                if (staTopicNo == stuTopicNo) {
                    for (Topic t : topics) {
                        Integer topicBegin = t.getTopicBegin();
                        Integer topicEnd = t.getTopicEnd();
                        if (staTopicNo >= topicBegin && staTopicNo <= topicEnd) {
                            if (t.getTopicType() == 1) {// 单选题
                                if (!StringUtil.stringIsNull(staAnswer)
                                        && !StringUtil.stringIsNull(stuAnswer)
                                        && staAnswer.equals(stuAnswer)) {
                                    studentGrage += t.getTopicPoint();
                                }
                            } else {// 多选题
                                if (!StringUtil.stringIsNull(staAnswer)
                                        && staAnswer.equals(stuAnswer)) {// 完全正确
                                    studentGrage += t.getTopicPoint();
                                } else if (!StringUtil.stringIsNull(staAnswer)
                                        && !StringUtil.stringIsNull(stuAnswer)) {// 缺选
                                    int staA = answerToInt(staAnswer);
                                    int stuA = answerToInt(stuAnswer);
                                    if ((staA & stuA) == stuA) {
                                        studentGrage += t.getTopicPartPoint();
                                    }
                                }
                            }
                        }
                    }

                }
            }
        }
        return studentGrage;
    }

    /**
     * 获取学生的成绩
     *
     * @param topics
     * @param staList
     * @param stuList
     * @return
     */
    public synchronized static float getStudentGradeForList(List<Topic> topics, List<Map<String, Object>> staList,
                                                            List<ObjectAnswer> stuList) {
        float studentGrage = 0;
        if (topics == null || staList == null || stuList == null) {
            return studentGrage;
        }
        for (int i = 0; i < staList.size(); i++) {// 遍历标准
//            StandardAnswer standardAnswer = staList.get(i);
            Map<String, Object> m = staList.get(i);
            String staAnswer = (String) m.get(ANSWER);
//            String staAnswer = standardAnswer.getAnswer();
            int staTopicNo = (Integer) m.get(SERIAL_NUMBER);
//            int staTopicNo = standardAnswer.getSerialNumber();
            for (int j = 0; j < stuList.size(); j++) {// 遍历学生的答案
//                Map<String, Object> stuMap = stuList.get(j);
                ObjectAnswer objectAnswer = stuList.get(j);
                String stuAnswer = objectAnswer.getAnswer();
                int stuTopicNo = objectAnswer.getSerialNumber();
//                String stuAnswer = (String) stuMap.get(ANSWER);
//                int stuTopicNo = (Integer) stuMap.get(SERIAL_NUMBER);
                if (staTopicNo == stuTopicNo) {
                    for (Topic t : topics) {
                        Integer topicBegin = t.getTopicBegin();
                        Integer topicEnd = t.getTopicEnd();
                        if (staTopicNo >= topicBegin && staTopicNo <= topicEnd) {
                            if (t.getTopicType() == 1) {// 单选题
                                if (!StringUtil.stringIsNull(staAnswer)
                                        && !StringUtil.stringIsNull(stuAnswer)
                                        && staAnswer.equals(stuAnswer)) {
                                    studentGrage += t.getTopicPoint();
                                }
                            } else {// 多选题
                                if (!StringUtil.stringIsNull(staAnswer)
                                        && staAnswer.equals(stuAnswer)) {// 完全正确
                                    studentGrage += t.getTopicPoint();
                                } else if (!StringUtil.stringIsNull(staAnswer)
                                        && !StringUtil.stringIsNull(stuAnswer)) {// 缺选
                                    int staA = answerToInt(staAnswer);
                                    int stuA = answerToInt(stuAnswer);
                                    if ((staA & stuA) == stuA) {
                                        studentGrage += t.getTopicPartPoint();
                                    }
                                }
                            }
                        }
                    }

                }
            }
        }
        return studentGrage;
    }

    /**
     * 将不包含主观题答案的list转为包含主观题答案（为0）的list
     *
     * @param standardAnswers
     * @param maxSeqno
     * @param topics
     */
    public static List<StandardAnswer> standaAnswerToUpload(List<StandardAnswer> standardAnswers,
                                                            int maxSeqno, List<Topic> topics) {
        List<StandardAnswer> answerList = new ArrayList<StandardAnswer>();
        int count = 0;
        for (int seqno = 1; seqno <= maxSeqno; seqno++) {
            StandardAnswer answer = new StandardAnswer();
            if (getCurrentTopicType(seqno - 1, topics) == 3) {
                answer.setAnswer("0");
                answer.setSerialNumber(seqno - 1);
            } else {
                answer.setAnswer(standardAnswers.get(count).getAnswer());
                answer.setSerialNumber(seqno - 1);
                count++;
            }
            answerList.add(answer);
        }
        return answerList;
    }

    /**
     * 将content转换成List<Map<String,Object>>
     *
     * @param answerMap
     * @param topics
     * @return
     */
    public static ArrayList<StandardAnswer> mapToStandardList(
            Map<String, Object> answerMap, List<Topic> topics) {
        ArrayList<StandardAnswer> standardAnswers = null;
        if (topics != null) {
            standardAnswers = new ArrayList<StandardAnswer>();
            for (Topic t : topics) {
                if (t.getTopicType() != 3) {// 只要不是主观题
                    Integer topicBegin = t.getTopicBegin();
                    Integer topicEnd = t.getTopicEnd();
                    for (int i = topicBegin; i <= topicEnd; i++) {
                        StandardAnswer standardAnswer = new StandardAnswer();
                        List<String> answerList = (List<String>) answerMap.get(i + "");
                        String answer = "";
                        if (answerList != null) {
                            for (String s : answerList) {
                                answer += s;
                            }
                        }
                        standardAnswer.setSerialNumber(i);
                        standardAnswer.setAnswer(answer == null ? "" : answer);
                        standardAnswers.add(standardAnswer);
                    }
                }
            }
        }
        return standardAnswers;
    }

    /**
     * 获取一个默认空的题目答案content字符串
     *
     * @param topics
     * @return
     */
    public static String topicsToAnswerContent(List<Topic> topics) {
        StringBuilder answerContent = new StringBuilder();
        for (Topic t : topics) {
            if (t.getTopicType() == 3) {
                continue;
            }
            Integer topicBegin = t.getTopicBegin();
            Integer topicEnd = t.getTopicEnd();
            for (int startNo = topicBegin; startNo <= topicEnd; startNo++) {
                answerContent.append("0").append(",");
            }
        }
        String content = "";
        if (answerContent.toString().endsWith(",")) {
            content = answerContent.toString().substring(0,
                    answerContent.toString().length() - 1);
        }
        return content;
    }

    /**
     * 获取当前选择的题目的类型
     *
     * @param topicNo 当前题目的题号
     * @param topics  题型集合
     * @return
     */

    public static int getCurrentTopicType(int topicNo, List<Topic> topics) {
        // 1.单选 ，2.多选 3.主观题
        int type = 0;
        for (Topic t : topics) {
            Integer topicBegin = t.getTopicBegin();
            Integer topicEnd = t.getTopicEnd();
            if (topicNo >= topicBegin && topicNo <= topicEnd) {
                type = t.getTopicType();
                break;
            }
        }
        return type;
    }

    /**
     * 解码，将1，2，4，8转换为 A,B,C,D
     *
     * @param sAnswer
     * @return
     */
    public static String decryptAnswer(String sAnswer) {
        if (null == sAnswer)
            return "";
        String sDecryptAnswer = "";
        String[] sItemAnswer = sAnswer.split(",");
        int nItemCnt = sItemAnswer.length;
        for (int i = 0; i < nItemCnt; i++) {
            if (0 < i)
                sDecryptAnswer += ",";
            int nAns;
            if ("" == sItemAnswer[i])
                nAns = 0;
            else
                nAns = StringUtil.parseInt(sItemAnswer[i], 0);
            for (int j = 0; j < 8; j++) {
                int nBit = nAns & (int) Math.pow(2, j); // 同小题答案取与
                if (0 < nBit) {
                    sDecryptAnswer += intToStr(nBit);
                }
            }
        }
        return sDecryptAnswer;
    }

    /**
     * 编码，将A,B,C,D转换为1，2，4，8
     *
     * @param sItemAnswer
     * @return
     */
    private static String encryptAnswer(String sItemAnswer) {
        // 将答案加密
        if (sItemAnswer.endsWith(",")) {
            sItemAnswer += " ";// 防止 ,,,,,,,切割不了
        }
        String sEnAnswer = "";
        String[] arrAnswer = sItemAnswer.split(",");
        int nItemCnt, nLen, nResult;
        nItemCnt = arrAnswer.length;
        for (int i = 0; i < nItemCnt; i++) {
            nResult = 0;
            char[] arrChrAns = arrAnswer[i].toCharArray();
            nLen = arrChrAns.length;
            for (int j = 0; j < nLen; j++) {
                nResult |= letterToInt(arrChrAns[j]);
            }
            if (0 < i) {
                sEnAnswer += ",";
            }
            sEnAnswer += nResult + "";
        }

        return sEnAnswer;
    }

    /**
     * 将一道题的答案转换为int类型
     *
     * @param answer
     * @return
     */
    public static int answerToInt(String answer) {
        int result = 0;
        if (answer == null) {
            return result;
        }
        char[] charArray = answer.toCharArray();
        for (char c : charArray) {
            result |= letterToInt(c);
        }
        return result;
    }

    private static int letterToInt(char ch) {
        if ('A' > ch || 'H' < ch)
            return 0;

        int nBitOff = ch - 'A';
        return 1 << nBitOff;
    }

    /**
     * 根据
     *
     * @param nBit
     * @return
     */
    private static String intToStr(int nBit) {
        String sLetter;
        switch (nBit) {
            case 1:
                sLetter = "A";
                break;
            case 2:
                sLetter = "B";
                break;
            case 4:
                sLetter = "C";
                break;
            case 8:
                sLetter = "D";
                break;
            case 16:
                sLetter = "E";
                break;
            case 32:
                sLetter = "F";
                break;
            case 64:
                sLetter = "G";
                break;
            case 128:
                sLetter = "H";
                break;
            default:
                sLetter = "";
                break;
        }
        return sLetter;
    }
}